//
//  HWGameDetailLikeCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/3/15.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit

class HWGameDetailLikeCell: UITableViewCell {
    var collectionView:UICollectionView!
    var model:HWGameDetailInfoModel? {
        didSet{
            self.collectionView.reloadData()
        }
    }
    var jumpToGameDetail:((_:String)->())?
    class func cellWithTableView(tableView:UITableView) -> (HWGameDetailLikeCell) {
        let reUse:String = "HWGameDetailLikeCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HWGameDetailLikeCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HWGameDetailLikeCell
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
        initFrame()
    }
    
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = UIColor.clear
        
        let flowL = UICollectionViewFlowLayout.init()
        flowL.minimumLineSpacing = 7*widthScale
        flowL.minimumInteritemSpacing = 7*widthScale
        flowL.scrollDirection = .horizontal
        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: flowL)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.backgroundColor = .clear
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.showsVerticalScrollIndicator = false
        collectionView.register(HWGameDetailLikeCollectionCell.self, forCellWithReuseIdentifier: "HWGameDetailLikeCollectionCell")
        contentView.addSubview(collectionView)
        
    }
    
    func initFrame() -> Void {
        collectionView.snp.makeConstraints { make in
            make.left.right.top.bottom.equalToSuperview().priority(.high)
            make.height.equalTo(102*widthScale)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
extension HWGameDetailLikeCell:UICollectionViewDelegateFlowLayout,UICollectionViewDelegate,UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        //        self.jumpToLiveRoom?(models?[indexPath.row] ?? HWGameLiveListModel())
        self.jumpToGameDetail?(model?.cnlike[indexPath.row].id ?? "")
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets.zero
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return model?.cnlike.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: screenWidth/4, height: 102*widthScale)
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HWGameDetailLikeCollectionCell", for: indexPath) as! HWGameDetailLikeCollectionCell
        cell.model = model?.cnlike[indexPath.row]
        return cell
    }
    
}
class HWGameDetailLikeCollectionCell:UICollectionViewCell {
    var gameImageView:UIImageView!
    var titleLabel:UILabel!
    var model:HWGameDetailCnlikeModel?{
        didSet {
            gameImageView.sd_setImage(with: URL(string: model?.icon ?? ""), completed: nil)
            titleLabel.text = model?.name
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func initUI() -> Void {
        gameImageView = UIImageView()
        gameImageView.layer.cornerRadius = 8*widthScale
        gameImageView.layer.masksToBounds = true
        gameImageView.backgroundColor = .clear
        gameImageView.contentMode = .scaleAspectFill
        contentView.addSubview(gameImageView)
        
        titleLabel = HWLabel.createLabel(title: "", color: "#888888", font: 10*widthScale)
        titleLabel.textAlignment = .center
        titleLabel.numberOfLines = 1
        contentView.addSubview(titleLabel)
    }
    
    private func initFrame() -> Void {
        gameImageView.snp.makeConstraints { make in
            make.top.equalTo(18*widthScale)
            make.centerX.equalToSuperview()
            make.width.height.equalTo(52*widthScale)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(5*widthScale)
            make.right.equalTo(-5*widthScale)
            make.top.equalTo(gameImageView.snp.bottom).offset(5*widthScale)
        }
    }
}
